package com.bnz.manager.service.impl;

import com.bnz.manager.entity.group.Specification;
import com.bnz.manager.entity.SpecificationOptionEntity;
import com.bnz.manager.service.SpecificationOptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bnz.common.utils.PageUtils;
import com.bnz.common.utils.Query;

import com.bnz.manager.dao.SpecificationDao;
import com.bnz.manager.entity.SpecificationEntity;
import com.bnz.manager.service.SpecificationService;

/**
 * @author: zhanghao
 * @date: 2023/2/18 17:37
 * @param:
 * @return:
 * @功能:
 **/
@Service("specificationService")
public class SpecificationServiceImpl extends ServiceImpl<SpecificationDao, SpecificationEntity> implements SpecificationService {

    @Autowired
    private SpecificationDao specificationDao;
    @Autowired
    private SpecificationOptionService optionService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpecificationEntity> page = this.page(
                new Query<SpecificationEntity>().getPage(params),
                new QueryWrapper<SpecificationEntity>()
        );

        return new PageUtils(page);
    }

/**
 * 1.添加组合类对象specification
 */
    @Override
    public void insert(Specification specification) {
        SpecificationEntity specificationEntity = new SpecificationEntity();

        specificationEntity.setSpecName(specification.getSpecName());

        this.save(specificationEntity);

//        1.2 添加规格选项
        List<SpecificationOptionEntity> options = specification.getOptions();
        for (SpecificationOptionEntity option : options) {
            // 1.2.1 绑定规则选项的外键
            option.setSpecId(specificationEntity.getId());

            // 1.3 完成添加
            optionService.save(option);
        }


    }
/**
 * @author: zhanghao
 * @date: 2023/2/18 16:45
 * @param: [id]
 * @return: com.bnz.manager.entity.group.Specification
 * @功能: 根据规格id查询出规格及规格选项列表
 **/
    @Override
    public Specification findById(String id) {
        //1.1 查询出规格信息
        SpecificationEntity specificationEntity = this.getById(id);

        List<SpecificationOptionEntity> options = optionService.list(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id", id));

        return new Specification(specificationEntity.getId(), specificationEntity.getSpecName(), options);
    }

    /**
     * @author: zhanghao
     * @date: 2023/2/18 17:06
     * @param: [specification]
     * @return: void
     * @功能: 修改规格及规格选项
     * 注意：一对多或多对多，多的一方一般先根据一方的外键删除所有
     **/
    @Override
    public void update(Specification specification) {
        // 1. 先更新规格
        SpecificationEntity entity = new SpecificationEntity(specification.getId(), specification.getSpecName());
        this.updateById(entity);
        // 2. 再删除规格选项
        optionService.remove(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id", specification.getId()));

        // 3. 添加规格选项
        for (SpecificationOptionEntity option : specification.getOptions()) {
            option.setSpecId(specification.getId());
            optionService.save(option);
        }


    }

    @Override
    public void deleteByIds(List<String> ids) {
        //4.1 先删除规格
        specificationDao.deleteBatchIds(ids);
        // 4.2 在删除规格选项
        for (String id : ids) {
            optionService.remove(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id", id));
        }
    }


}